package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.common.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.vo.TeacherQueryVo;
import com.atguigu.guli.service.edu.feign.OssFileService;
import com.atguigu.guli.service.edu.mapper.TeacherMapper;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @className: TeacherServiceImpl
 * @description: 讲师 服务实现类
 * @date: 2020/11/18
 * @author: cakin
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Autowired
    private OssFileService ossFileService;

    /**
     * 功能描述：显示分页查询列表
     *
     * @param pageParam      分页查询参数
     * @param teacherQueryVo 条件查询参数
     * @return IPage<Teacher> 分页信息
     * @author cakin
     * @date 2020/11/18
     */
    @Override
    public IPage<Teacher> selectPage(Page<Teacher> pageParam, TeacherQueryVo teacherQueryVo) {
        // 1 排序：按照sort字段排序
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        // 2 有分页查询 无条件查询
        if (teacherQueryVo == null) {
            return baseMapper.selectPage(pageParam, queryWrapper);
        }
        // 3 构造条件查询
        String name = teacherQueryVo.getName();
        Integer level = teacherQueryVo.getLevel();
        String joinDateBegin = teacherQueryVo.getJoinDateBegin();
        String joinDateEnd = teacherQueryVo.getJoinDateEnd();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.likeRight("name", name);
        }
        if (level != null) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(joinDateBegin)) {
            queryWrapper.ge("join_date", joinDateBegin);
        }
        if (!StringUtils.isEmpty(joinDateEnd)) {
            queryWrapper.le("join_date", joinDateEnd);
        }
        // 2 有分页查询 有条件查询
        return baseMapper.selectPage(pageParam, queryWrapper);
    }

    /**
     * 功能描述：根据关键字查询讲师名列表
     *
     * @param key 搜索的关键字
     * @return List<Map < String, Object>> 讲师名列表
     * @author cakin
     * @date 2020/11/28
     */
    @Override
    public List<Map<String, Object>> selectNameList(String key) {
        // 构造查询列和查询条件
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name");
        queryWrapper.likeRight("name", key);
        // 执行查询
        List<Map<String, Object>> list = baseMapper.selectMaps(queryWrapper);
        return list;
    }

    /**
     * 功能描述：根据讲师id删除图像
     *
     * @param id 讲师id
     * @return boolean 是否删除成功
     * @author cakin
     * @date 2020/11/29
     */
    @Override
    public boolean removeAvatarById(String id) {
        // 根据id获取讲师avatar 的 url
        Teacher teacher = baseMapper.selectById(id);
        if (teacher != null) {
            String avatar = teacher.getAvatar();
            if (!StringUtils.isEmpty(avatar)) {
                R r = ossFileService.removeFile(avatar);
                return r.getSuccess();
            }
        }
        return false;
    }
}
